package com.itheima.web;

import com.itheima.pojo.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version V1.0
 * @author: yongjichu
 * @date: 2020/2/9 10:51
 * @description:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/getUsername")
    public Result getUsername(){
        /**
         * 当我们登陆之后，利用security框架的上下文对象获取用户名称
         *
         */
        //这里获得的principal就是一个user对象，里面含有用户名称，密码，还有用户的权限集合
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //直接返回数据
        return Result.success(true,"",principal.getUsername());
    }
}
